﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DoAn_BumTruong_DesignPattern
{
    class ArticleController : Abstract
    {
        public List<Article> SelectAll()
        {
            using (var db = new DBContextEntity())
            {
                var query = from ar in db.Articles
                            select ar;
                return query.ToList();
            }
        }
        //Paging
        public List<Article> Paging(string page, out int howManyPage)
        {
            int pageSize = GlobalConfiguration.PageSize;
            howManyPage = (int)Math.Ceiling((double)SelectAll().Count / (double)pageSize);
            return SelectAll().Skip(pageSize * Convert.ToInt32(page) - pageSize).Take(pageSize).ToList();
        }
        //PagingByMenuID
        public List<Article> PagingByMenuID(string menuID, string page, out int howManyPage)
        {
            int mnID = Convert.ToInt32(menuID);
            int pageSize = GlobalConfiguration.PageSize;
            List<Article> list = SelectAll().Where(ar => ar.MenuID.Equals(mnID)).ToList();
            howManyPage = (int)Math.Ceiling((double)list.Count / (double)pageSize);
            return list.Skip(pageSize * Convert.ToInt32(page) - pageSize).Take(pageSize).ToList();
        }
        //Paging theo ID
        //public Article SelectByArticleID(string articleID)
        //{
        //    int articleIDD = Convert.ToInt32(articleID);
        //    using (var db = new DBContextEntity())
        //    {
        //        return db.Articles.SingleOrDefault(ar => ar.ArticleID == articleIDD);
        //    }
        //}
        public List<Article> SelectByArticleID(string articleID)
        {
            int articleIDD = Convert.ToInt32(articleID);
            using (var db = new DBContextEntity())
            {
                var query = from ar in db.Articles
                            where ar.ArticleID.Equals(articleIDD)
                            select ar;
                return query.ToList();
            }
        }
        public List<Article> SearchName(string title)
        {
            List<Article> list = SelectAll().FindAll(
            delegate(Article ar)
            {
                if (SearchUnsigned.ConvertToString(ar.Title.ToLower()).Contains(title))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
            return list;
        }
        //PagingByMenuID
        public List<Article> PagingBySearchName(string title, string page, out int howManyPage)
        {
            int mnID = Convert.ToInt32(title);
            int pageSize = GlobalConfiguration.PageSize;
            List<Article> list = SelectAll().FindAll(
            delegate(Article ar)
            {
                if (SearchUnsigned.ConvertToString(ar.Title.ToLower()).Contains(title))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
            howManyPage = (int)Math.Ceiling((double)list.Count / (double)pageSize);
            return list.Skip(pageSize * Convert.ToInt32(page) - pageSize).Take(pageSize).ToList();
        }
        public override bool Add(object o)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(object o)
        {
            throw new NotImplementedException();
        }

        public override bool Update(object o)
        {
            throw new NotImplementedException();
        }

        public override bool UpdateActive(int id, bool state)
        {
            throw new NotImplementedException();
        }

        public override bool UpdateOrderNo(int id, int order)
        {
            throw new NotImplementedException();
        }

        public override bool UpdatePosition(int id, int order)
        {
            throw new NotImplementedException();
        }

        public override bool UpdateMenuType(int id, int type)
        {
            throw new NotImplementedException();
        }
    }
}